
(function () {
    var core = ecui,
        util = core.util,
        ui = ecui.ui,
        dom = ecui.dom;

    var clearTimer = util.blank;
    function getBranchBank () {
        var num = this.$ID.split('-').pop(),
            bank = ecui.get('bank-' + num),
            city = ecui.get('bank-city-' + num),
            branchBank = ecui.get('branch-bank-' + num);
        
        branchBank.setValue('');
        branchBank.setText('');
        branchBank._oItems = [];
        if (bank.getValue().trim() && city.getValue().trim()) {
            ecui.esr.request(
                util.stringFormat('branch@JSON /erp-management/basic/subbanks?bankCode={0}&fullCityCode={1}', bank.getValue(), city.getValue()),
                function () {
                    var branch = ecui.esr.getData('branch'),
                        code = ecui.esr.getData('branch_CODE');
                    if (code === 0) {
                        branchBank._oItems = branch.map(function (item) {
                            return {
                                id: item.branchNo,
                                name: item.name
                            };
                        });
                    }
                }.bind(this),
                function () {}.bind(this)
            )
        }
    }
    Object.assign(
        NS.data,
        {
        }
    );
    Object.assign(
        NS.ui,
        {
            AccountTypeSelectEdit: ecui.inherits(
                mao.ui.SelectEdit,
                {
                    onchange: function (event) {
                        mao.ui.SelectEdit.prototype.onchange.call(this, event);
                        this.changeHandler();
                    },
                    onready: function () {
                        util.timer(function () {
                            if (this.getMain()) {
                                this.changeHandler();
                            }
                        }, 30, this);
                    },
                    changeHandler: function () {
                        var value = this.getValue(),
                            form = this.getInput().form,
                            accountName = form.elements.accountName.getControl(),
                            accountCode = dom.children(dom.next(accountName.getParent().getMain()))[1].getControl(),
                            bankCode = dom.children(dom.next(accountCode.getParent().getMain()))[1].getControl(),
                            cityFullCode = dom.children(dom.next(bankCode.getParent().getMain()))[1].getControl(),
                            subBankCode = dom.children(dom.next(cityFullCode.getParent().getMain()))[1].getControl();
        
                        // 开户行、省市、分支行
                        [bankCode, cityFullCode, subBankCode].forEach(function (item) {
                            var control = item;
                            control[value === '1' ? 'enable' : 'disable']();
                            control.getParent()[value === '1' ? 'show' : 'hide']();
                        });
        
                        if (accountCode) {
                            accountCode[value !== '4' ? 'enable' : 'disable']();
                            accountCode.getParent()[value !== '4' ? 'show' : 'hide']();
                        }
                    }
                }
            ),
            BankCitySelectEdit: ecui.inherits(
                mao.ui.SelectEdit,
                {
                    onchange: function (event) {
                        mao.ui.SelectEdit.prototype.onchange.call(this, event);
                        clearTimer();
                        clearTimer = util.timer(function () {
                            getBranchBank.call(this);
                        }, 300, this);
                    },
                    init: function () {
                        mao.ui.SelectEdit.prototype.init.call(this, event);
                        clearTimer();
                        clearTimer = util.timer(function () {
                            getBranchBank.call(this);
                        }, 300, this);
                    }
                }
            ),
            SaveAccountBtn: ecui.inherits(
                ui.Button,
                {
                    onclick: function (event) {
                        this.request(
                            'data@FORM /erp-management/account/save?editAccountForm',
                            function () {
                                var code = ecui.esr.getData('data_CODE');
                                var data = ecui.esr.getData('data');
                                if (code === 0) {
                                    // ecui.tip('success', this._sOptId ? '更新账户成功' : '新增账户成功');

                                    event.item = data;
                                    history.go(-2);
                                    util.timer(function () {
                                        var route = ecui.esr.getRoute(ecui.esr.getLocation().split('~')[0]);
                                        var main = ecui.$(route.main);
                                        var form = dom.toArray(document.forms).find(function (item) { return dom.contain(main, item) });

                                        ecui.dispatchEvent(form.elements.accountId.getControl(), 'change', event);
                                    }, 300, this);
                                    
                                }
                            }.bind(this),
                            function (err) {
                                if (!err) {
                                    ecui.tip('warn', '请完善必填信息');
                                }
                                return false;
                            }
                        );
                    }
                },
                ui.Submit
            )
        }
    );
    ecui.esr.addRoute('edit', {
        model: [''],
        weight: 9991,
        onbeforerequest: function (context) {
            context.num = mao.util.getTimeRandom();
        },
        onbeforerender: function (context) {
        },
        onafterrender: function (context) {
        }
    });
}());

